import {createOrModify, find} from '@/services/system-parameter-api';
import {
  PageContainer,
  ProCard,
  ProColumns,
  ProDescriptions,
  ProDescriptionsItemProps
} from '@ant-design/pro-components';
import {Button, message} from 'antd';
import React, {useState} from 'react';
import UpdateForm from "./components/UpdateForm";


/**
 * @en-US Update node
 * @zh-CN 更新节点
 *
 * @param fields
 */
const handleUpdate = async (fields: API.SystemParameter) => {
  const hide = message.loading('更新中');
  try {
    const res = await createOrModify(fields);
    hide();

    if (res) {
      message.success('更新成功');
      return true;
    }
    return false;
  } catch (error) {
    hide();
    return false;
  }
};


const SystemParameter: React.FC = () => {

  /**
   * @en-US The pop-up window of the distribution update window
   * @zh-CN 分布更新窗口的弹窗
   * */
  const [updateModalOpen, handleUpdateModalOpen] = useState<boolean>(false);

  const [currentRow, setCurrentRow] = useState<API.SystemParameter>();


  const commonColumns: ProColumns<API.SystemParameter>[] = [
    {
      title: '系统schema',
      dataIndex: 'systemSchema',
      hideInSearch: true,
    },
    {
      title: '双向同步标记表',
      dataIndex: 'systemMarkTable'
    },
    {
      title: '双向同步标记id列名',
      dataIndex: ['systemMarkTableColumn'],
      hideInSearch: true,
    },
    {
      title: '双向同步标记info列名',
      dataIndex: ['systemMarkTableInfo'],
      hideInSearch: true,
    },
    {
      title: '同步系统buffer表',
      dataIndex: ['systemBufferTable'],
      hideInSearch: true,
    },
    {
      title: '同步系统dual表',
      dataIndex: ['systemDualTable'],
      hideInSearch: true,
    },
    {
      title: '下载方式',
      dataIndex: ['retriever'],
      hideInSearch: true,
    },
    {
      title: '默认报警联系人',
      dataIndex: ['defaultAlarmReceiver'],
      renderText:(_,record) => (record?.defaultAlarmReceiveKey || '') + (record?.defaultAlarmReceiver || '')
    },
    {
      title: '自定义报警联系人',
      dataIndex: ['alarmReceiver'],
      renderText:(_,record) => (Object.entries(record?.alarmReceiver)?.map(([k, v]) => k + '=' + v)?.join(";") || '')
    }
  ]

  const descriptionColumns: ProColumns<API.SystemParameter>[] = [
    ...commonColumns,
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <Button
          key={"op"}
          type={'primary'}
          onClick={() => {
            handleUpdateModalOpen(true);
            setCurrentRow(record);
          }}
        >
          编辑
        </Button>
      ]
    }

  ];

  return (
    <PageContainer>
      <ProCard style={{width:600}}>
        <ProDescriptions<API.SystemParameter>
          title={'系统参数'}
          column={1}
          request={async () => find()}
          columns={descriptionColumns as ProDescriptionsItemProps<API.SystemParameter>[]}
        />
        <UpdateForm
          onSubmit={async (value) => {
            const success = await handleUpdate(value);
            if (success) {
              handleUpdateModalOpen(false);
              setCurrentRow(undefined);
            }
          }}
          onCancel={() => {
            handleUpdateModalOpen(false);
            setCurrentRow(undefined);
          }}
          updateModalOpen={updateModalOpen}
          values={currentRow}
        />
      </ProCard>

    </PageContainer>
  );
};

export default SystemParameter;
